<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="85px"
      class="form-search"
    >
      <SearchTool
        :code="{
          factory: 'factoryCode',
          workshop: 'workshopCode',
          line: 'lineCode',
          section: 'sectionCode',
        }"
        v-model:factory="queryParams.factoryCode"
        v-model:workshop="queryParams.workshopCode"
        v-model:line="queryParams.lineCode"
        v-model:section="queryParams.sectionCode"
      ></SearchTool>

      <el-form-item label="线边仓名称" prop="storehouseName">
        <el-input
          v-model="queryParams.storehouseName"
          placeholder="请输入线边仓名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable filterable>
          <el-option
            v-for="dict in sys_use_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['storehouse:storehouse:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['storehouse:storehouse:edit']"
        >
          编辑
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['storehouse:storehouse:remove']"
        >
          删除
        </el-button>
      </el-col> -->

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="线边仓编号"
        align="center"
        prop="storehouseCode"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">
            {{ scope.row.storehouseCode }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="线边仓名称"
        align="center"
        prop="storehouseName"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="工厂"
        align="center"
        prop="factoryName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="工段"
        align="center"
        prop="workshopName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="产线"
        align="center"
        prop="lineName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="工艺段"
        align="center"
        prop="sectionName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="负责人"
        align="center"
        prop="leaderName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_use_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['storehouse:storehouse:edit']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['storehouse:storehouse:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="title"
      draggable
      v-model="open"
      :close-on-click-modal="false"
      width="700px"
      append-to-body
    >
      <el-form ref="formRef" :model="form" :rules="rules" class="form-search" label-width="100px">
        <el-form-item label="线边仓编号" prop="storehouseCode">
          <code-input
            v-model="form.storehouseCode"
            code="LINESIDE_WAREHOUSE_CODE"
            :disabled="!!form.id"
            placeholder="请输入线边仓编号"
          />
        </el-form-item>
        <el-form-item label="线边仓名称" prop="storehouseName">
          <el-input v-model="form.storehouseName" placeholder="请输入线边仓名称" />
        </el-form-item>
        <SearchTool
          :code="{
            factory: 'factoryCode',
            workshop: 'workshopCode',
            line: 'lineCode',
            section: 'sectionCode',
          }"
          :itemStyle="{ width: '100%' }"
          v-model:factory="form.factoryCode"
          v-model:factoryName="form.factoryName"
          v-model:workshop="form.workshopCode"
          v-model:workshopName="form.workshopName"
          v-model:line="form.lineCode"
          v-model:lineName="form.lineName"
          v-model:section="form.sectionCode"
          v-model:sectionName="form.sectionName"
        ></SearchTool>

        <el-form-item label="负责人" prop="leaderName">
          <UserSelect
            v-model="form.leaderName"
            v-model:code="form.leaderId"
            placeholder="请选择负责人"
            title="选择负责人"
          ></UserSelect>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_use_status" :key="dict.value" :value="dict.value">
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="建仓日期" prop="buildDate">
          <el-date-picker
            v-model="form.buildDate"
            value-format="YYYY-MM-DD"
            type="date"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :maxlength="200"
            show-word-limit
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dict">
import {
  listStorehouse,
  getStorehouse,
  delStorehouse,
  addStorehouse,
  updateStorehouse,
} from "@/api/storehouse/storehouse";
import DetailData from "./data";
import { ref, reactive, getCurrentInstance, toRefs } from "vue";
const { proxy } = getCurrentInstance();
const { sys_use_status } = proxy.useDict("sys_use_status");

const typeList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    factoryCode: undefined,
    workshopCode: undefined,
    lineCode: undefined,
    sectionCode: undefined,
    storehouseCode: undefined,
    storehouseName: undefined,
    leaderName: undefined,
    status: undefined,
  },
  rules: {
    storehouseCode: [{ required: true, message: "请输入线边仓编号!", trigger: "change" }],
    storehouseName: [{ required: true, message: "请输入线边仓名称!", trigger: "blur" }],
    factoryCode: [{ required: true, message: "请选择工厂!", trigger: "change" }],
    workshopCode: [{ required: true, message: "请选择工段!", trigger: "change" }],
    lineCode: [{ required: true, message: "请选择产线!", trigger: "change" }],
    leaderId: [{ required: true, message: "请输入负责人!", trigger: "change" }],
    status: [{ required: true, message: "请选择状态!", trigger: "change" }],
    buildDate: [{ required: true, message: "请输入建仓日期!", trigger: "change" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询字典类型列表 */
function getList() {
  loading.value = true;
  listStorehouse(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    typeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleDetail(row) {
  proxy.subPage(DetailData, { title: "线边仓详情", data: row });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    factoryCode: undefined,
    workshopCode: undefined,
    lineCode: undefined,
    sectionCode: undefined,
    storehouseCode: undefined,
    storehouseName: undefined,
    leaderId: undefined,
    leaderName: undefined,
    status: undefined,
    remark: undefined,
  };
  proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增线边仓";
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const dictId = row.id || ids.value;
  getStorehouse(dictId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "编辑线边仓信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if(!form.value.sectionCode){
        form.value.sectionCode = ''
        form.value.sectionName = ''
      }
      if (form.value.id != undefined) {
        updateStorehouse(form.value).then(() => {
          proxy.$modal.msgSuccess("编辑成功");
          open.value = false;
          getList();
        });
      } else {
        addStorehouse(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const dictIds = row.id || ids.value;
  const storehouseCodes =
    row.storehouseCode ||
    ids.value.map((id) => typeList.value.find((item) => item.id === id)?.storehouseCode).join(", ");

  proxy.$modal
    .confirm('是否确认删除线边仓编号为"' + storehouseCodes + '"的数据项？')
    .then(function () {
      return delStorehouse(dictIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
